package com.woniuxy.controller;

import com.woniuxy.entity.ExamMenu;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.ExamMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags="查询科目菜单")
@RestController
@RequestMapping("/examMenu")
public class ExamMenuControllor {
    @Autowired
    ExamMenuService examMenuService;

    @ApiOperation("查询科目菜单")
    @GetMapping("/findAll")
    public ResponseEntity findAll(){
        return examMenuService.findAll();
    }

    @ApiOperation("查询所有题库")
    @GetMapping("/findAll/{currentPage}/{pageSize}")
    public ResponseEntity findAll(@PathVariable int currentPage, @PathVariable int pageSize){
        return  examMenuService.findAll(currentPage, pageSize);
    }

    @ApiOperation("题库删除")
    @DeleteMapping("/deleteOne/{id}")
    public ResponseEntity deleteOne(@PathVariable int id){
        System.out.println(id);
        return examMenuService.deleteOne(id);
    }

    @ApiOperation("点击修改用id查找")
    @GetMapping("/findById/{id}")
    public ResponseEntity findById(@PathVariable int id){
        System.out.println(id);
        return examMenuService.findById(id);
    }

    @ApiOperation("题库修改")
    @PutMapping("/updateOne")
    public ResponseEntity updateOne(@RequestBody ExamMenu examMenu){
        System.out.println(examMenu);
        return examMenuService.updateOne(examMenu);
    }

    @ApiOperation("题库新增")
    @PostMapping("/addOne")
    public ResponseEntity addOne(@RequestBody ExamMenu examMenu){
        System.out.println(examMenu);
        int fid = examMenu.getFid();
        if (fid == 0){
            return examMenuService.addOneNoFid(examMenu);
        }else {
            return examMenuService.addOne(examMenu);
        }
    }

    @ApiOperation("题库搜索")
    @PostMapping("/findBySearch/{currentPage}/{pageSize}")
    public ResponseEntity findBySearch(@RequestBody Map<String,String> map,@PathVariable int currentPage, @PathVariable int pageSize){
        System.out.println("传过来的名字" + map);
        for (String name : map.values()) {
            System.out.println(name);
            return examMenuService.findBySearch(name, currentPage, pageSize);
        }
        return null;
    }

//    @PostMapping("/find/")
//    public ResponseEntity findBySearch(){
//
//        return examMenuService.clcAdd();
//    }

    @ApiOperation("点击新建题库查找出fid为null的父级")
    @GetMapping("/clcAdd")
    public ResponseEntity clcAdd(){
        return examMenuService.clcAdd();
    }

    @ApiOperation("批量删除")
    @PostMapping("/deleteMany")
    public ResponseEntity deleteMany(@RequestBody ExamMenu examMenu){
        System.out.println(examMenu);
        int[] ids = examMenu.getIds();
        for (int i:ids) {
            examMenuService.deleteOne(i);
        }
        return null;
    }

    @ApiOperation("找出fid为不为null的，没有子级的试卷类别")
    @GetMapping("/findCategory")
    public ResponseEntity findCategory(){
        return examMenuService.findCategory();
    }

    @ApiOperation("查找所有fid")
    @GetMapping("/findFid")
    public ResponseEntity findFid(){
        return examMenuService.findFid();
    }

    @ApiOperation("查找登录学生所有参加的考试试卷")
    @GetMapping("/findAllpaper/{currentPage}/{pageSize}/{id}")
    public ResponseEntity findAllpaper(@PathVariable int currentPage, @PathVariable int pageSize, @PathVariable int id){
        return examMenuService.findAllpaper(currentPage, pageSize, id);
    }

    @ApiOperation("按搜索查找登录学生所参加的考试试卷")
    @PostMapping("/findAllpaperBySearch/{currentPage}/{id}")
    public ResponseEntity findAllpaperBysearch(@PathVariable int currentPage,@PathVariable int id,@RequestBody Map<String,String> map){
        System.out.println("传过来的名字" + map);
        System.out.println("传过来的当前页" + currentPage);
        for (String name : map.values()) {
            System.out.println(name);
            return examMenuService.findAllpaperBysearch(currentPage, id, name);
        }
        return null;
    }
}
